package com.platform.cdcs.fragment.custom;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.cdcs.R;
import com.platform.cdcs.model.BaseObjResponse;
import com.platform.cdcs.model.CustomerItem;
import com.platform.cdcs.model.DisProductList;
import com.platform.cdcs.model.PriceItem;
import com.platform.cdcs.model.RefershEvent;
import com.platform.cdcs.tool.Constant;
import com.platform.cdcs.tool.FragmentUtil;
import com.sherchen.slidetoggleheader.views.ObservableXListView;
import com.trueway.app.uilib.adapter.EnhancedAdapter;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.model.ChooseItem;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.sourceforge.zbar.Orientation;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import okhttp3.Call;

/**
 * Created by holytang on 2017/10/15.
 */
public class PriceListFragment extends BaseFragment {

    private ItemAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initView(View view) {
        hideThisToolBar(view);
        initLoadImg(view.findViewById(R.id.load));
        setHasOptionsMenu(true);
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        setTitle("价格列表");
        ObservableXListView slideListView = (ObservableXListView) view.findViewById(android.R.id.list);
        ArrayList<PriceItem> list = (ArrayList<PriceItem>) getArguments().getSerializable("model");

//        TextView textView = new TextView(getContext());
//        textView.setText("为该型号产品添加新价格");
//        textView.setTextSize(14);
//        textView.setGravity(Gravity.CENTER);
//        textView.setBackgroundResource(R.drawable.btn_green);
//        textView.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
//        int paddingx = Utils.convertDIP2PX(getContext(), 10);
//        textView.setPadding(paddingx, paddingx, paddingx, paddingx);
//        textView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                FragmentUtil.navigateToInNewActivity(getActivity(), AddPriceFragment.class, getArguments());
//            }
//        });
//        LinearLayout rootView = new LinearLayout(getContext());
//        rootView.setOrientation(LinearLayout.VERTICAL);
//        rootView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        lp.setMargins(paddingx, paddingx, paddingx, paddingx);
//        textView.setLayoutParams(lp);
//        rootView.addView(textView);


//        RelativeLayout headerView = new RelativeLayout(getContext());
//        headerView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.header_bg));
//        AbsListView.LayoutParams llp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
//        headerView.setLayoutParams(llp);
//        int padding = Utils.convertDIP2PX(getContext(), 7);
//        headerView.setPadding(padding * 2, padding, padding * 2, padding);
//        textView = new TextView(getContext());
//        textView.setText("产品单价");
//        textView.setTextSize(12);
//        textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_gray));
//        headerView.addView(textView);
//
//        textView = new TextView(getContext());
//        RelativeLayout.LayoutParams tLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//        tLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
//        textView.setLayoutParams(tLP);
//        textView.setText("单位");
//        textView.setTextSize(12);
//        textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_gray));
//        headerView.addView(textView);
//        rootView.addView(headerView);
//        slideListView.addHeaderView(rootView);
        slideListView.setPullRefreshEnable(false);
        slideListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("model", (PriceItem) adapterView.getItemAtPosition(i));
                FragmentUtil.navigateToInNewActivity(getActivity(), PriceDetailFragment.class, bundle);
            }
        });
        adapter = new ItemAdapter(getContext());
        adapter.addAll(list);
        slideListView.setAdapter(adapter);
//        request();
    }

    @Subscribe
    public void onEventMainThread(RefershEvent event) {
        if (event.mclass == getClass()) {
            request();
        }
    }

    @Override
    public int layoutId() {
        return R.layout.listview;
    }

    private void request() {
        showLoadImg();
        Map<String, String> param = new HashMap<>();
        param.put("cusCode", getArguments().getString("cusCode"));
        param.put("itemCode", getArguments().getString("itemCode"));
        getHttpClient().post().url(Constant.DIST_PRODUCT_PRICE_LST).params(Constant.makeParam(param)).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                dismissLoadImg();
                Type type = new TypeToken<BaseObjResponse<PriceItem.PriceList>>() {
                }.getType();
                BaseObjResponse<PriceItem.PriceList> response = new Gson().fromJson(s, type);
                if ("1".equals(response.getResult().getCode())) {
                    adapter.clear();
//                    adapter.addAll(response.getResult().getPriceList());
                    adapter.addAll(response.getResult().getInvoicePriceList());
                    adapter.notifyDataSetChanged();
                } else {
                    Utils.showToast(getContext(), response.getResult().getMsg());
                }
            }
        });
    }

    private class ItemAdapter extends EnhancedAdapter<PriceItem> {

        public ItemAdapter(Context context) {
            super(context);
        }

        @Override
        protected void bindView(View paramView, Context paramContext, int position) {
            ViewHolder holder = (ViewHolder) paramView.getTag();
            PriceItem item = getItem(position);
            holder.titleView.setText(item.getCusName());
            if (!TextUtils.isEmpty(item.getCusCode())) {
                holder.textView.setText(item.getCusCode());
            } else {
                holder.textView.setText(item.getDistCode());
            }
            holder.descView.setText("¥" + item.getPrice());
        }

        @Override
        protected View newView(Context context, int position, ViewGroup viewgroup) {
            View rootView = inflater.inflate(R.layout.four_item, viewgroup, false);
            ViewHolder holder = new ViewHolder();
            holder.titleView = (TextView) rootView.findViewById(R.id.title);
            rootView.findViewById(R.id.time).setVisibility(View.GONE);
            holder.textView = (TextView) rootView.findViewById(R.id.text);
            holder.descView = (TextView) rootView.findViewById(R.id.desc);
            holder.descView.setTextSize(14);
            holder.descView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_dark));
            rootView.setTag(holder);
            return rootView;
        }
    }

    private class ViewHolder {
        TextView titleView, textView, descView;
    }
}
